package com.changpay.zywy.ui.fragment.main

import android.text.TextUtils
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseMvpFragment
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.mvp.contract.IncomeTotalContract
import com.changpay.zywy.bean.EarningTotalDefaultBean
import com.changpay.zywy.mvp.presenter.IncomeTotalPresenter
import com.changpay.zywy.utils.DateUtil
import kotlinx.android.synthetic.main.fragment_income_persion.*
import java.util.*


class IncomePersonFragment : BaseMvpFragment<IncomeTotalContract.View, IncomeTotalContract.Presenter>(),
    IncomeTotalContract.View {


    private var start_date = ""
    private var end_date = ""
    private var selected_date = ""
    private var date_type = "day"
    private var date_type_index = 0
    private val default_day = 0
    private val default_week = 1
    private val default_month = 2
    private val search_day = 3
    private val search_week = 4
    private val search_month = 5

    override fun showEarning(data: EarningTotalDefaultBean) {
        setEarningData(data)
    }


    companion object {
        fun getInstance(): IncomePersonFragment = IncomePersonFragment()
    }

    override fun createPresenter(): IncomeTotalContract.Presenter = IncomeTotalPresenter()

    override fun attachLayoutRes(): Int = R.layout.fragment_income_persion

    override fun lazyLoad() {
        initDateData(date_type_index, "")
    }

    override fun initView(view: View) {
        super.initView(view)
        income_total_date_rg.setOnCheckedChangeListener { group, checkedId ->
            if (selected_date.isEmpty()) {
                when (checkedId) {
                    R.id.income_total_day_rb -> initDateData(default_day, selected_date)
                    R.id.income_total_week_rb -> initDateData(default_week, selected_date)
                    R.id.income_total_month_rb -> initDateData(default_month, selected_date)
                }
            } else {
                when (checkedId) {
                    R.id.income_total_day_rb -> initDateData(search_day, selected_date)
                    R.id.income_total_week_rb -> initDateData(search_week, selected_date)
                    R.id.income_total_month_rb -> initDateData(search_month, selected_date)
                }
            }
        }

    }

    private fun loadData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["type"] = "2"
        param["date_type"] = date_type
        param["search_date_star"] = start_date
        param["search_date_end"] = end_date
        mPresenter?.getEarning(param)
    }

    private fun setEarningData(data: EarningTotalDefaultBean) {
        val start_empty = TextUtils.isEmpty(start_date)
        val end_empty = TextUtils.isEmpty(end_date)
        // 其中一个为空时,默认请求
        if (start_empty || end_empty) {
            data.let {
                income_total_yesterday_ll.visibility = View.VISIBLE
                income_total_circlePercentView_two.visibility = View.VISIBLE
                income_yesterday_tv.visibility = View.VISIBLE
                val now = it.earnings_info?.now!!
                val past = it.earnings_info?.past!!
                val nowTotal = GetCount(
                    now.personal_activation!!,
                    now.personal_earnings!!,
                    now.other_earnings!!
                ).toFloat()
                if (nowTotal != 0f) {
                    val sweepTwo = now.personal_activation!!.toDouble() / nowTotal
                    val sweepThree = now.other_earnings!!.toDouble() / nowTotal
                    val sweepFour = now.personal_earnings!!.toDouble() / nowTotal
                    val persentList: List<Double> = listOf(sweepTwo,sweepThree,sweepFour)
                    val NumberList: List<String> = listOf(now.personal_activation!!,now.other_earnings!!,
                        now.personal_earnings!!)
                    // 设置数据
                    income_total_circlePercentView_one.setThreeData(
                        persentList,NumberList
                    )
                    income_total_today_tv.text = "￥$nowTotal"
                } else {

                    val persentList: List<Double> = listOf(0.0,0.0,0.0)
                    val NumberList: List<String> = listOf(now.personal_activation!!,now.other_earnings!!,
                        now.personal_earnings!!)
                    income_total_circlePercentView_one.setThreeData(
                        persentList,NumberList
                    )
                    income_total_today_tv.text = "￥$nowTotal"
                }
                val pastTotal = GetCount(
                    past.personal_activation!!,
                    past.personal_earnings!!,
                    past.other_earnings!!
                ).toFloat()
                if (pastTotal != 0f) {
                    val sweepTwo = past.personal_activation!!.toDouble() / pastTotal
                    val sweepThree = past.other_earnings!!.toDouble() / pastTotal
                    val sweepFour = past.personal_earnings!!.toDouble() / pastTotal

                    val persentList: List<Double> = listOf(sweepTwo,sweepThree,sweepFour)
                    val NumberList: List<String> = listOf(past.personal_activation!!,past.other_earnings!!,past.personal_earnings!!)
                    // 设置数据
                    income_total_circlePercentView_two.setThreeData(
                        persentList,NumberList
                    )
                    income_total_yesterday_tv.text = "￥$pastTotal"
                } else {
                    val persentList: List<Double> = listOf(0.0,0.0,0.0)
                    val NumberList: List<String> = listOf(past.personal_activation!!,past.other_earnings!!,
                        past.personal_earnings!!)
                    income_total_circlePercentView_two.setThreeData(
                        persentList,NumberList
                    )
                    income_total_yesterday_tv.text = "￥$pastTotal"
                }
            }
        } else {
            data.let {
                income_total_yesterday_ll.visibility = View.INVISIBLE
                income_total_circlePercentView_two.visibility = View.INVISIBLE
                income_yesterday_tv.visibility = View.INVISIBLE
                val search = it.earnings_info?.search!!
                val searchTotal = GetCount(
                    search.personal_activation!!,
                    search.personal_earnings!!,
                    search.other_earnings!!
                ).toFloat()
                if (searchTotal != 0f) {
                    val sweepTwo = search.personal_activation!!.toDouble() / searchTotal
                    val sweepThree = search.other_earnings!!.toDouble() / searchTotal
                    val sweepFour = search.personal_earnings!!.toDouble() / searchTotal


                    val persentList: List<Double> = listOf(sweepTwo,sweepThree,sweepFour)
                    val NumberList: List<String> = listOf(search.personal_activation!!,search.other_earnings!!,search.personal_earnings!!)
                    // 设置数据
                    income_total_circlePercentView_one.setThreeData(
                        persentList,NumberList
                    )
                    income_total_today_tv.text = "￥$searchTotal"
                } else {
                    val persentList: List<Double> = listOf(0.0,0.0,0.0)
                    val NumberList: List<String> = listOf(search.personal_activation!!,search.other_earnings!!,
                        search.personal_earnings!!)
                    income_total_circlePercentView_one.setThreeData(
                        persentList,NumberList
                    )
                    income_total_today_tv.text = "￥$searchTotal"
                }
            }
        }
    }


    fun setTime(selectTime: String) {
        selected_date = selectTime
        when (date_type_index) {
            default_day , search_day -> initDateData(search_day, selected_date)
            default_month , search_month -> initDateData(search_month, selected_date)
            default_week , search_week -> initDateData(search_week, selected_date)
        }
    }

    fun GetCount(
        leftTopNumber: String,
        rightTopNumber: String,
        leftBottomNumber: String
    ): Double {
        val aDouble = java.lang.Double.valueOf(rightTopNumber)
        val aDouble1 = java.lang.Double.valueOf(leftBottomNumber)
        val aDouble2 = java.lang.Double.valueOf(leftTopNumber)
        return aDouble + aDouble1 + aDouble2
    }

    private fun initDateData(type: Int, date: String) {
        date_type_index = type
        when (type) {
            default_day -> {
                income_total_yesterday_ll.visibility = View.VISIBLE
                income_total_circlePercentView_two.visibility = View.VISIBLE
                income_yesterday_tv.visibility = View.VISIBLE
                date_type = "day"
                start_date = date
                end_date = date
                income_today_tv.setText("今天")
                income_yesterday_tv.setText("昨天")
                loadData()
            }
            default_week -> {
                income_total_yesterday_ll.visibility = View.VISIBLE
                income_total_circlePercentView_two.visibility = View.VISIBLE
                income_yesterday_tv.visibility = View.VISIBLE
                date_type = "week"
                val NowDate = DateUtil.getPastDate(0)
                val pastDate = DateUtil.getPastDate(6)
                val pastStartWeekDate = DateUtil.getPastDate(7)
                val pastEndWeekDate = DateUtil.getPastDate(13)
                start_date = date
                end_date = date
                income_today_tv.setText(pastDate + " 至 " + NowDate)
                income_yesterday_tv.setText(pastEndWeekDate + " 至 " + pastStartWeekDate)
                loadData()
            }
            default_month -> {
                income_total_yesterday_ll.visibility = View.VISIBLE
                income_total_circlePercentView_two.visibility = View.VISIBLE
                income_yesterday_tv.visibility = View.VISIBLE
                date_type = "month"
                start_date = date
                end_date = date
                val instance = Calendar.getInstance()
                val firstDayOfMonth1 =
                    DateUtil.getFirstDayOfMonth1(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH))
                val lastDayOfMonth1 =
                    DateUtil.getLastDayOfMonth1(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH))
                val firstDayOfMonth2 =
                    DateUtil.getFirstDayOfMonth1(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH) - 1)
                val lastDayOfMonth2 =
                    DateUtil.getLastDayOfMonth1(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH) - 1)
                income_today_tv.setText(firstDayOfMonth1 + " 至 " + lastDayOfMonth1)
                income_yesterday_tv.setText(firstDayOfMonth2 + " 至 " + lastDayOfMonth2)
                loadData()
            }
            search_day -> {
                income_total_yesterday_ll.visibility = View.INVISIBLE
                income_total_circlePercentView_two.visibility = View.INVISIBLE
                income_yesterday_tv.visibility = View.INVISIBLE
                date_type = "day"
                start_date = date
                end_date = date
                income_today_tv.setText(date)
                loadData()
            }
            search_week -> {
                income_total_yesterday_ll.visibility = View.INVISIBLE
                income_total_circlePercentView_two.visibility = View.INVISIBLE
                income_yesterday_tv.visibility = View.INVISIBLE
                date_type = "week"
                start_date = DateUtil.getSpecifiedDayBefore(date, 6)
                end_date = date
                income_today_tv.setText("$start_date 至 $end_date")
                loadData()
            }
            search_month -> {
                income_total_yesterday_ll.visibility = View.INVISIBLE
                income_total_circlePercentView_two.visibility = View.INVISIBLE
                income_yesterday_tv.visibility = View.INVISIBLE
                date_type = "month"
                val split = date.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                if (split.size != 3) {
                    showToast("获取时间错误")
                    return
                }
                start_date = DateUtil.getFirstDayOfMonth1(Integer.valueOf(split[0]), Integer.valueOf(split[1]) - 1)
                end_date = DateUtil.getLastDayOfMonth1(Integer.valueOf(split[0]), Integer.valueOf(split[1]) - 1)
                income_today_tv.setText("$start_date 至 $end_date")
                loadData()
            }
            else  // 默认请求day
            -> {
                date_type = "day"
                income_today_tv.setVisibility(View.VISIBLE)
                income_yesterday_tv.setVisibility(View.VISIBLE)
            }
        }
    }
}